import A from '../../../../common/typeActions'
import SQLite from '../../../../common/SQLiteHelper'
import ICE from '../../../../common/ICEHelper'
import {action, mutation} from '../../../../common/quantity'

const config = require('../../../../config/config')
const skin = require('../../../../config/skin')
const commonServer = require('../../../../server/common/commonServer')

// const skin = require('../../config/skin')

const state = {
  preplanOptions: null,
  tempData: {},
  isBrowser: process.browser,
  userTree: []
}

const actions = {
  // 清理案件信息
  async [A.COMMON.ClearCaseInfo](context) {
    await commonServer.clearDataStore()
    context.dispatch(A.SLIDEEDITOR.ClearSlideEditor)
  },
  async [action.common.initProgram]({dispatch, commit}) {
    try {
      let options = localStorage.getItem('preplanOptions')
      if (!options) {
        options = JSON.stringify({
          isAddZjcl: true,
          isMergeDir: true,
          isAddWjnr: false
        })
      }

      commit(mutation.common.setPreplanOptions, JSON.parse(options))

      try {
        // await dispatch(action.common.setUserTree)
      } catch (e) {
        console.error('获取单位树数据出错', e)
      }
    } catch (e) {
      throw e
    }
  },
  async [action.common.setUserTree](context) {
    try {
      let unitCode = skin[skin.useSkin].app.unitCode
      // console.log("开始获取单位")
      let userTree = await ICE.GetUnitTreeList({DWBM: unitCode})

      context.commit(mutation.common.setUserTree, userTree)
    } catch (e) {
      throw e
    }
  },
  async [A.COMMON.UpdateDBFile](context, path) {
    try {
      context.commit('SET_LOADINGCASE', {loading: true, loadingText: '正在验证案件文件版本，请稍等...'})
      // let version = skin[skin.useSkin].app.version
      let versionInfos = {}
      if (process.browser) {
        versionInfos = await this._vm.$api.post.getVersionRecords(path)
      } else {
        versionInfos = await ICE.GetVersionRecords(path)
      }

      if (versionInfos.versionUpdates && versionInfos.versionUpdates.length) {
        await this._vm.$confirm(
          `当前版本无法打开该案件文件，是否升级案件文件？`,
          '提示'
        )
        /* 升级后案件文件将无法在【${version}】以下版本打开 */
        await SQLite.ExecuteNonQuery({executeSql: versionInfos.versionUpdates, isTransaction: true}, path)
      }

      if (versionInfos.versionInfos && versionInfos.versionInfos.length) {
        let executeSql = []
        versionInfos.versionInfos.forEach((item) => {
          executeSql.push(item.sql)
        })
        if (executeSql.length) await SQLite.ExecuteNonQuery({executeSql: executeSql, isTransaction: true}, path)
      }
      return true
    } catch (error) {
      if (error.message) await this._vm.$alert(error.message, '升级案件文件时发生了一个错误', {type: 'error'})
      return false
    } finally {
      context.commit('SET_LOADINGCASE', {loading: false})
    }
  }
}

const mutations = {
  [mutation.common.setPreplanOptions](state, options) {
    state.preplanOptions = options
    localStorage.setItem('preplanOptions', JSON.stringify(options, null, 2))
  },
  [mutation.common.setTempData](state, tempData = {}) {
    state.tempData = tempData
  },
  [mutation.common.setUserTree](state, userTree = []) {
    state.userTree = userTree
  }
}

export default {
  state,
  mutations,
  actions
}
